#-*- coding:utf-8 -*-
import os
import sys
from ShortMessage import MSG

def getPageRange(range_list_str,split_list_str):
    '''
    获取页面范围，如果没有指定，则返回(0,-1)代表整本书
    '''
    handle_all_book = False
    select_range = []
    #处理参数-ss
    if split_list_str:
        _slist = sorted([1]+[int(x) for x in split_list_str.split(",")])
        for i in  range(len(_slist)-1):
            select_range.append((_slist[i]-1,_slist[i+1]-2))
        select_range.append((_slist[-1]-1,-1))
    #处理参数-s
    if range_list_str:
        for section in range_list_str.split(","):
            ses = section.split("-")
            if len(ses)  <= 1:
                MSG("Need Both Start page and End page,not %s"%section)
            else:
                select_range.append((int(ses[0])-1,int(ses[1])-1))
    #未指定区间，则处理整本书
    if not select_range:
        handle_all_book = True
        select_range.append((0,-1))
    return select_range,handle_all_book

def getMarginRatios(ratios_str):
    '''
    从输入字符串中，获取页面边缘比例，如果输入少于4个，会自动使用最后一个填充缺失比例
    '''
    rect = []
    try:
        pts = [float(x) for x in ratios_str.split(",")]
        rect =(pts+[pts[-1]]*4)[:4]
    except:
        pass
    return rect